package cc.mrbird.unit.service.impl;

import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.exception.FileImportException;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.dao.ContactInforMapper;
import cc.mrbird.unit.dao.UnitInfoMapper;
import cc.mrbird.unit.dao.UserUnitMapper;
import cc.mrbird.unit.domain.ContactInfor;
import cc.mrbird.unit.domain.UnitInfo;
import cc.mrbird.unit.domain.UserUnit;
import cc.mrbird.unit.service.ContactInforService;
import cc.mrbird.unit.service.UnitInfoService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("contactInforService")
public class ContactInforServiceImpl extends BaseService<ContactInfor> implements ContactInforService {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ContactInforMapper contactInforMapper;
    @Autowired
    private UnitInfoMapper unitInfoMapper;
    @Autowired
    private UserUnitMapper userUnitMapper;
    @Override
    public List<ContactInfor> findContactInforWithRole(ContactInfor contactInfor, QueryRequest request) {
        try {
            return this.contactInforMapper.findContactInforWithRole(contactInfor);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public ContactInfor findByName(String contact_name) {
        Example example = new Example(ContactInfor.class);
        example.createCriteria().andCondition("lower(contact_name)=", contact_name.toLowerCase());
        List<ContactInfor> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void add(ContactInfor contactInfor,User user) {
        UserUnit unitIdByUser = userUnitMapper.findUnitIdByUser(user.getUserId());
        UnitInfo unitInfo = unitInfoMapper.findByUnitId(unitIdByUser.getUnit_id());
        contactInforMapper.save(contactInfor);
        Long relevant_id = contactInfor.getRelevant_id();
        unitInfoMapper.updateContact_id(relevant_id,unitInfo.getUnit_id());
    }

    @Override
    public void update(ContactInfor contactInfor) {
        this.updateNotNull(contactInfor);
    }

    @Override
    public ContactInfor findById(Long id) {
        try {
            return this.contactInforMapper.findById(id);
        } catch (Exception e) {
            log.error("error", e);
            return new ContactInfor();
        }
    }

    @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "relevant_id", ContactInfor.class);
    }
}
